﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool.Commands.BatchCalibrationBoard
{
    public enum RGBSel
    {
        eRGB_R_ON = 0x11,
        eRGB_G_ON = 0x22,
        eVacuum_Pump_ON = 0x44,

        eRGB_R_OFF = 0x01,
        eRGB_G_OFF = 0x02,
        eVacuum_Pump_OFF = 0x04,

        eRGB_R_Flashing = 0x81,
        eRGB_G_Flashing = 0x82,
    }

    public class BatchCaliBoardCMDsSwitchRGB : BatchCaliBoardCMDsBase
    {
        private RGBSel _rgbOp;

        public BatchCaliBoardCMDsSwitchRGB(SerialPortsBase serialPortManager, string commandName, RGBSel rgbOp) : base(serialPortManager, commandName)
        {
            _rgbOp = rgbOp;
        }

        protected override async Task SendCMD()
        {
            byte[] cmdBytes = new byte[8] { 0xAA, 0x55, 0x03, 0x01, 0x00, 0x00, 0x0D, 0x0A};

            cmdBytes[4] = (byte)_rgbOp;
            cmdBytes[5] = (byte)(cmdBytes[0] + cmdBytes[1] + cmdBytes[2] + cmdBytes[3] + cmdBytes[4]);

            SerialPortsBase.PrintMessage("--> " + Utils.BytesArrayToHexString(cmdBytes, suffix: " "));

            await SerialPortMager.SerialPortWrite(cmdBytes);
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => {
                SerialPortsBase.PrintMessage("<-- " + $"{Utils.HexToString(data)}");
            });
        }

        public override void OnFailure()
        {

        }

        public override void OnSuccess()
        {

        }

        public override void OnTimeout()
        {

        }
    }

    public class AutoCaliBatchCaliBoardCMDsSwitchRGB : BatchCaliBoardCMDsSwitchRGB
    {
        public AutoCaliBatchCaliBoardCMDsSwitchRGB(SerialPortsBase serialPortManager, string commandName, RGBSel rgbOp) : base(serialPortManager, commandName, rgbOp)
        { 
        
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => {
                CommandExeSuccess();
                SetCurrentCMDResponseFinishedFlg();
                SerialPortsBase.PrintMessage("AutoCaliBatchCaliBoardCMDsSwitchRGB <-- " + $"{Utils.HexToString(data)}");
            });
        }
    }
}
